﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Helper;
using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using 无人值守地磅称重系统.Dialogs;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Views;
using CompanyEntiey = Company.Sqlite.Models.Company;

namespace 无人值守地磅称重系统.ViewModels
{
    internal class CompanyViewModel : ObservableObject
    {
        public CompanyEntiey NewCompany  { get; set;}  = new CompanyEntiey();

        private List<CompanyEntiey> companies;

        public List<CompanyEntiey> Companies { get => companies; set => SetProperty(ref companies, value); }

        public ICommand InsertCommand { get; }

        public ICommand UpdateCommand { get; }

        public ICommand DeleteCommand { get; }

        public ICommand SearchCommand { get; }

        public ICommand ClearKeywordCommand { get; }

        public ISession Session { get; }

        public ICompanyRepository CompanyRepository { get; }

        public CompanyViewModel(ISession session, ICompanyRepository companyRepository)
        {
            Session= session;
            CompanyRepository= companyRepository;
            Companies = CompanyRepository.GetAll();

            InsertCommand = new RelayCommand(Insert);
            UpdateCommand = new RelayCommand<CompanyEntiey>(Update);
            DeleteCommand = new RelayCommand<CompanyEntiey>(Delete);
            SearchCommand = new RelayCommand<string>(Search);
            ClearKeywordCommand = new RelayCommand<TextBox>(ClearKeyword);

        }

        private void ClearKeyword(TextBox textBox)
        {
            textBox.Text = string.Empty;
            CompanyRepository.GetAll();
        }

        private void Search(string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                Companies = CompanyRepository.GetAll();
            }
            else
            {
                Companies = CompanyRepository.GetAll(keyword);
            }
        }


        private async void Delete(CompanyEntiey company)
        {
            var view = new Dialog("是否执行删除操作？", System.Windows.MessageBoxButton.YesNo);
            bool result = (bool)await DialogHost.Show(view, "RootDialog");

            if(result)
            {
                int count = CompanyRepository.Delete(company);
                if(count > 0)
                {
                    Session.Show("删除成功！");
                    Companies= CompanyRepository.GetAll();
                }
            }
        }

        private async void Update(CompanyEntiey company)
        {
            var newEntity = company.DeepClone();

            var view = new UpdateCompanyView
            {
                DataContext = new UpdateCompanyViewModel(newEntity)
            };

            bool result = (bool)await DialogHost.Show(view, "RootDialog");

            if (result)
            {
                if (string.IsNullOrEmpty(newEntity.Name))
                {
                    Session.Show("请输入公司名称");
                    return;
                }

                if (string.IsNullOrEmpty(newEntity.Contact))
                {
                    Session.Show("请输入联系人");
                    return;
                }


                if (string.IsNullOrEmpty(newEntity.Telephone))
                {
                    Session.Show("请输入联系电话");
                    return;
                }

                company.Name = newEntity.Name;
                company.Contact = newEntity.Contact;
                company.Telephone = newEntity.Telephone;

                int count = CompanyRepository.Update(company);
                if (count > 0)
                {
                    Session.Show("修改成功！");
                    Companies = CompanyRepository.GetAll();
                }
            }
        }


        private void Insert()
        {
            if (string.IsNullOrEmpty(NewCompany.Name))
            {
                Session.Show("请输入公司名称");
                return;
            }

            if (string.IsNullOrEmpty(NewCompany.Contact))
            {
                Session.Show("请输入联系人");
                return;
            }


            if (string.IsNullOrEmpty(NewCompany.Telephone))
            {
                Session.Show("请输入联系电话");
                return;
            }

            if(CompanyRepository.Select(NewCompany.Name) !=null)
            {
                Session.Show("该公司已存在");
                return;
            }

            CompanyEntiey company = new CompanyEntiey();
            company.Name = NewCompany.Name;
            company.Contact = NewCompany.Contact;
            company.Telephone = NewCompany.Telephone;

            int count = CompanyRepository.Insert(company);
            
            if(count > 0)
            {
                Session.Show("添加成功");
                Companies= CompanyRepository.GetAll();
                NewCompany.Name = string.Empty;
                NewCompany.Contact= string.Empty;
                NewCompany.Telephone= string.Empty;
                Companies = CompanyRepository.GetAll();
            }
        }
    }
}
